# Maintainer: Milan P. Stanić <mps@arvanta.net>

_flavor=asahi
pkgname=linux-${_flavor}
pkgver=6.6_p15

_rel=${pkgver#*_p}
kver=${pkgver%_p*}
kver=${kver//_/-}
_asahiver=$kver-$_rel
pkgdesc="Apple Silicon (M1 development kernel)"
pkgrel=2
url="https://www.kernel.org"
depends="initramfs-generator speakersafetyd"
_depends_dev="perl gmp-dev elfutils-dev flex bison"
makedepends="$_depends_dev sed bc linux-headers installkernel
	linux-firmware-any openssl-dev diffutils findutils
	clang16-libclang
	rust
	rustfmt
	rust-bindgen
	rust-src
	"
options="!strip !check"
_config=${config:-config-asahi.${CARCH}}
subpackages="$pkgname-dev:_dev:$CBUILD_ARCH"
source="linux-asahi-$_asahiver.tar.gz::https://github.com/AsahiLinux/linux/archive/refs/tags/asahi-$_asahiver.tar.gz
	config-asahi.aarch64

	385ea7b5023486aba7919cec8b6b3f6a843a1013.patch
	6a24102c06c95951ab992e2d41336cc6d4bfdf23.patch
	"

builddir="$srcdir/linux-asahi-$_asahiver"
arch="aarch64"
license="GPL-2.0-only"

unset _flavors
for _i in $source; do
	case $_i in
	config-*.$CARCH)
		_f=${_i%.$CARCH}
		_f=${_f#config-}
		_flavors="$_flavors ${_f}"
		if [ "linux-$_f" != "$pkgname" ]; then
			subpackages="$subpackages linux-${_f}::$CBUILD_ARCH linux-${_f}-dev:_dev:$CBUILD_ARCH"
		fi
		;;
	esac
done

_carch=${CARCH}
case "$_carch" in
aarch64*) _carch="arm64" ;;
arm*) _carch="arm" ;;
esac

prepare() {
	default_prepare
	rm "$builddir"/localversion*
	echo "-$_rel-asahi" > "$builddir"/localversion-alpine
	oldconfig
}

oldconfig() {
	for i in $_flavors; do
		local _config=config-$i.${CARCH}
		local _builddir="$srcdir"/build-$i
		mkdir -p "$_builddir"

		cp "$srcdir"/$_config "$_builddir"/.config
		make -C $builddir \
			O="$_builddir" \
			ARCH="$_carch" \
			listnewconfig oldconfig
	done
}

build() {
	unset LDFLAGS
	export KBUILD_BUILD_TIMESTAMP="$(date -Ru${SOURCE_DATE_EPOCH:+d @$SOURCE_DATE_EPOCH})"
	for i in $_flavors; do
		local _builddir="$srcdir"/build-$i
		make -C "$srcdir"/build-$i \
			ARCH="$_carch" CC="${CC:-gcc}" \
			KBUILD_BUILD_VERSION="$((pkgrel + 1 ))-Alpine"
	done
}

_package() {
	local _buildflavor="$1" _outdir="$2"
	export KBUILD_BUILD_TIMESTAMP="$(date -Ru${SOURCE_DATE_EPOCH:+d @$SOURCE_DATE_EPOCH})"

	cd "$srcdir"/build-$_buildflavor
	local _abi_release="$(make -s kernelrelease)"

	mkdir -p "$_outdir"/boot "$_outdir"/lib/modules

	make -j1 modules_install zinstall dtbs_install \
		ARCH="$_carch" \
		INSTALL_MOD_PATH="$_outdir" \
		INSTALL_MOD_STRIP=1 \
		INSTALL_PATH="$_outdir"/boot \
		INSTALL_DTBS_PATH="$_outdir/boot/dtbs-$_buildflavor"

	rm -f -- "$_outdir"/lib/modules/${_abi_release}/build \
		"$_outdir"/lib/modules/${_abi_release}/source
	rm -rf -- "$_outdir"/lib/firmware

	install -D -m644 include/config/kernel.release \
		"$_outdir"/usr/share/kernel/$_buildflavor/kernel.release
}

# main flavor installs in $pkgdir
package() {
	depends="$depends linux-firmware-any"

	_package asahi "$pkgdir"
}

_dev() {
	local _flavor=$(echo $subpkgname | sed -E 's/(^linux-|-dev$)//g')
	local O="$srcdir/build-$_flavor"
	local _abi_release="$(make -C "$O" -s kernelrelease)"
	# copy the only the parts that we really need for build 3rd party
	# kernel modules and install those as /usr/src/linux-headers,
	# simlar to what ubuntu does
	#
	# this way you dont need to install the 300-400 kernel sources to
	# build a tiny kernel module
	#
	pkgdesc="Headers and script for third party modules for $_flavor kernel"
	depends="$_depends_dev"
	local dir="$subpkgdir"/usr/src/linux-headers-${_abi_release}
	export KBUILD_BUILD_TIMESTAMP="$(date -Ru${SOURCE_DATE_EPOCH:+d @$SOURCE_DATE_EPOCH})"

	# kernel sources
	cd "$builddir"

	msg "Installing build files"
	install -Dt "$dir" -m644  Makefile localversion* \
		"$O/.config" "$O/Module.symvers" "$O/System.map" \
		"$O/vmlinux"
	install -Dt "$dir"/kernel -m644 kernel/Makefile
	cp -t "$dir" -a scripts "$O/scripts"

	msg "Installing headers..."
	cp -t "$dir" -a include "$O/include"

	install -Dt "$dir/arch/$_carch" -m644 $builddir/arch/$_carch/Makefile
	install -Dt "$dir/arch/$_carch/kernel" -m644 "$O"/arch/$_carch/kernel/asm-offsets.s
	cp -t "$dir/arch/$_carch" -a arch/$_carch/include $O/arch/$_carch/include

	install -Dt "$dir/drivers/md" -m644 drivers/md/*.h
	install -Dt "$dir/net/mac80211" -m644 net/mac80211/*.h

	# https://bugs.archlinux.org/task/13146
	install -Dt "$dir/drivers/media/i2c" -m644 drivers/media/i2c/msp3400-driver.h

	# https://bugs.archlinux.org/task/20402
	install -Dt "$dir/drivers/media/usb/dvb-usb" -m644 drivers/media/usb/dvb-usb/*.h
	install -Dt "$dir/drivers/media/dvb-frontends" -m644 drivers/media/dvb-frontends/*.h
	install -Dt "$dir/drivers/media/tuners" -m644 drivers/media/tuners/*.h

	# https://bugs.archlinux.org/task/71392
	install -Dt "$dir/drivers/iio/common/hid-sensors" -m644 drivers/iio/common/hid-sensors/*.h

	msg "Installing KConfig files..."
	find . -name 'Kconfig*' -exec install -Dm644 {} "$dir/{}" \;

	msg "Removing unneeded architectures..."
	local arch
	for arch in "$dir"/arch/*/; do
		case $(basename "$arch") in
			$_carch) continue ;;
		esac
		echo " - Removing $(basename "$arch")"
		rm -r "$arch"
	done

	msg "Removing documentation"
	rm -r "$dir"/Documentation

	msg "Removing broken symlinks..."
	find -L "$dir" -type l -printf 'Removing %P\n' -delete

	msg "Removing loose objects..."
	find "$dir" -type f -name '*.o' -printf 'Removing %P\n' -delete

	msg "Stripping build tools..."
	local file
	find "$dir" -type f -perm -u+x ! -name vmlinux -print0 | while read -rd '' file; do
		case "$(file -bi "$file")" in
			application/x-sharedlib\;*)			# Libraries (.so)
				strip -v $STRIP_SHARED "$file" ;;
			application/x-archive\;*)				# Libraries (.a)
				strip -v $STRIP_STATIC "$file" ;;
			application/x-executable\;*)		 # Binaries
				strip -v $STRIP_BINARIES "$file" ;;
			application/x-pie-executable\;*) # Relocatable binaries
				strip -v $STRIP_SHARED "$file" ;;
		esac
	done

	msg "Stripping vmlinux..."
	strip -v $STRIP_STATIC "$dir/vmlinux"

	msg "Adding symlink..."
	mkdir -p "$subpkgdir"/lib/modules/${_abi_release}
	ln -sf /usr/src/linux-headers-${_abi_release} \
		"$subpkgdir"/lib/modules/${_abi_release}/build
}

sha512sums="
b261e3813f1c0f5bae3a24e7ed0dce41470cfe2ee965ab0121d41793aa08f72d0d91be633f7ba0317f265a6e98a7a134cf86a29e7d48dbb58c5f5f7a3a13cc53  linux-asahi-6.6-15.tar.gz
0ed687414c03c7ef83842b0bc1ff95ec4f2f73a7eb9b3ca28e0c3f78ad7f3f03cfcc5c1c216548d3f5ec2ff10f59314c44776418807959b04558632d7f683209  config-asahi.aarch64
0ceb12811db6c517871bf741605055a2c970d045de582e37d8f05d8717e4b26f995f06d479be5c2e6c08a6eb6d00bd3cce6ed9ea8e416afb39c664cf571f480c  385ea7b5023486aba7919cec8b6b3f6a843a1013.patch
ae7f79b79e695775ae81c2ef77b9009dfcee488e727fc2a543194a267c19dabf53f1d44bcc8bf8cfa3f388a84bf5658c1e0a8d291ec1005d668cfa5c7ee4fbb0  6a24102c06c95951ab992e2d41336cc6d4bfdf23.patch
"
